package com.upu173.music.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.andbase.library.app.base.AbBaseFragment;
import com.andbase.library.cache.AbImageCache;
import com.andbase.library.utils.AbAnimationUtil;
import com.andbase.library.utils.AbLogUtil;
import com.cpacm.FloatingMusicMenu;
import com.upu173.music.R;
import com.upu173.music.activity.MusicPlayActivity;
import com.upu173.music.global.PlayerData;
import com.upu173.music.service.MusicPlayerService;
import com.upu173.music.utils.MusicUtil;


public class AlbumViewFragment extends AbBaseFragment {

    private MusicPlayActivity activity;
    private View rootView = null;
    public ImageView albumView;
    private String currentPlayPath;
    private AbImageCache cache = null;
    private boolean isAlbumPlay = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.music_view_album, container, false);

        activity = (MusicPlayActivity)this.getActivity();
        cache = AbImageCache.getInstance(activity);

        albumView = (ImageView)rootView.findViewById(R.id.album_view);

        return rootView;
    }

    public void checkCurrentPlay(){
        if(!PlayerData.getCurrentPlaySong().getPath().equals(currentPlayPath)){
            if(cache.containKey(PlayerData.getCurrentPlaySong().getPath())){
                Bitmap bitmap =  cache.getBitmap(PlayerData.getCurrentPlaySong().getPath());
                if(bitmap!= null){
                    albumView.setImageBitmap(bitmap);
                }else{
                    albumView.setImageDrawable(getResources().getDrawable(R.drawable.ic_music_cover));
                }

            }else{
                Bitmap bitmap = MusicUtil.getSongTag(activity,PlayerData.getCurrentPlaySong().getPath(),true).getBitmap();
                cache.putBitmap(PlayerData.getCurrentPlaySong().getPath(),bitmap);
                if(bitmap!= null){
                    albumView.setImageBitmap(bitmap);
                }else{
                    albumView.setImageDrawable(getResources().getDrawable(R.drawable.ic_music_cover));
                }
            }
            currentPlayPath = PlayerData.getCurrentPlaySong().getPath();
        }
    }

    public void updateUI(){
        if(albumView!=null){
            if(MusicPlayerService.playerManagerReceiver != null && PlayerData.getCurrentPlaySong()!=null) {
                if (MusicPlayerService.playerManagerReceiver.isPlaying()) {
                    if(!isAlbumPlay){
                        isAlbumPlay = true;
                        AbAnimationUtil.playRotateAnimation(albumView, 5000, Animation.INFINITE, Animation.RESTART, new LinearInterpolator());
                    }
                }else{
                    isAlbumPlay = false;
                    albumView.clearAnimation();
                }
            }
        }
    }

    @Override
    public void onResume() {
        checkCurrentPlay();
        updateUI();
        super.onResume();
    }

}
